package com.tusuo.stds.ui.integrity;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.tusuo.stds.MyApp;
import com.tusuo.stds.R;
import com.tusuo.stds.di.DaggerScope;
import com.tusuo.stds.di.Injector;
import com.tusuo.stds.rest.ApiHelper;
import com.tusuo.stds.rest.Complete;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.ViewById;

import javax.inject.Inject;

import autodagger.AutoComponent;
import autodagger.AutoInjector;
import rx.Subscriber;

/**
 * Created by developer on 17-8-30.
 * 知识完整度Fragment
 */

@AutoComponent(
        dependencies = MyApp.class
)
@AutoInjector
@DaggerScope(IntegrityFragment.class)
@EFragment(R.layout.frag_integrity)
public class IntegrityFragment extends Fragment{

    @ViewById(R.id.frag_integrity_list)
    ListView mListView;

    @Inject
    ApiHelper mApiHelper;

    @FragmentArg("dataId")
    int mDataId;
    @FragmentArg("type")
    int mType;
    private IntegrityAdapter mAdapter;

    @AfterInject
    void initDI() {
        Injector.initIntegrityFragmentComponent().inject(this);
    }

    @AfterViews
    public void initData() {
        mApiHelper.getComplete(new GetCompleteSubscriber(),mDataId,mType);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(),IntegrityDetailActivity_.class);
                intent.putExtra("dataId",mDataId);
                intent.putExtra("type",mType);
                intent.putExtra("completeId",(int)mAdapter.getItemId(position));
                startActivity(intent);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
//        if (mDataId != 0 && mType != 0) {
//            initData();
//        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
//        if (!hidden && mDataId != 0 && mType != 0) {
//            initData();
//        }
    }

    private class GetCompleteSubscriber extends Subscriber<Complete> {

        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            System.out.println("error" + e);

        }

        @Override
        public void onNext(Complete complete) {
            mAdapter = new IntegrityAdapter(getActivity(), complete);
            mListView.setAdapter(mAdapter);
        }
    }
}
